
#ifndef MSGUTILS_BIT_WRITER_H
#define MSGUTILS_BIT_WRITER_H

#include <stdint.h>
#include <stddef.h>

namespace msgutils {
class IWriteBuffer;

class BitWriter {
 public:
  BitWriter(IWriteBuffer* writer_buffer);

  BitWriter(uint8_t* buffer, size_t size);

  template<size_t size>
  BitWriter(uint8_t (&buffer)[size]);

  ~BitWriter(void);

  template<class T>
  BitWriter& operator<<(const T& val);

  template<class T>
  void AddBlock(const T* block, size_t size);

  size_t BytesWrite(void) const;

 private:
  template<class T, bool is_arithmetic>
  friend struct WriteDispatch;

  template<class T>
  void Write(const T& val);

  template<class T, size_t size>
  void Write(const T (&array)[size]);

  void Write(const char* string);

  void EncodeNumber(size_t bits, const void* number_addr);

  void Construct(uint8_t* buffer, size_t size);

 private:
  IWriteBuffer* wbuf_;
  bool delete_wbuf_;
};

#include "bit_writer-inl.h"

} // namespace msgutils 

#endif // MSGUTILS_BIT_WRITER_H

